//
//  SearchListController.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/13.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYouSearchListController.h"
#import "LYouDetailController.h"
#import "LYouSearchListCell.h"
#import "LYouVideoModel.h"

@interface LYouSearchListController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) NSMutableArray *allDataArray;
@property(nonatomic,strong) UICollectionView *mainCollectionView;
@property(nonatomic,strong) NSString *categoryId;

@end

@implementation LYouSearchListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.mainCollectionView];
    self.allDataArray = [NSMutableArray arrayWithCapacity:0];
    self.mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestData:self.categoryId];
        [self.mainCollectionView.mj_header endRefreshing];
    }];
}

/** 请求数据 */
-(void)requestData:(NSString *)categoryId{
    self.categoryId = categoryId;
    [LYouLoadingView show];
    NSString *url = [NSString stringWithFormat:@"%@/show/%@",LYBASEURL,categoryId];
    
    [[LYouNetClient sharedInstance]NetRequestGETWithRequestURL:url WithParameter:nil WithReturnValeuBlock:^(NSURLSessionDataTask *task,id  _Nullable responseObject,NSArray *responseArray) {
        NSLog(@"responseArray=====%@",responseArray);
        [self.allDataArray removeAllObjects];
        for (NSDictionary *dic in responseArray) {
            LYouVideoModel *model = [[LYouVideoModel alloc]init];
            model.post_id = dic[@"id"];
            model.video_url = dic[@"video"];
            model.thumbnail_url = dic[@"cover"];
            [self.allDataArray addObject:model];
        }
        [self.mainCollectionView reloadData];
        [LYouLoadingView hide];
    } WithErrorBlock:^(NSURLSessionDataTask *task, NSError *error) {
        [LYouLoadingView hide];

    } WithFailureBlock:^{
        [LYouLoadingView hide];

    }];
    
}

#pragma mark - UICollectionViewDelegate
/**分区个数*/
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSInteger count = self.allDataArray.count/2+self.allDataArray.count%2;
    return count;
}
/**每个分区item的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger count = self.allDataArray.count/2+self.allDataArray.count%2;
    if (section==count-1) {
        NSInteger lastCount = self.allDataArray.count - (count-1)*2;
        if (lastCount%2==0) {
            return 2;
        }else{
            return 1;
        }
    }else{
        return 2;
    }
}
/**创建cell*/
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifer = @"searchListCellID";
    LYouSearchListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifer forIndexPath:indexPath];
    NSInteger index = indexPath.section*2+indexPath.item;
    cell.model = self.allDataArray[index];
    return cell;
}
/**点击某个cell*/
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了第%ldsection=%ld分item",(long)indexPath.section,indexPath.item);
    LYouDetailController *detailVC = [[LYouDetailController alloc]init];
    NSInteger index = indexPath.section*2+indexPath.item;
    detailVC.model = self.allDataArray[index];
    [self.navigationController pushViewController:detailVC animated:YES];
}


#pragma mark - 懒加载
-(UICollectionView *)mainCollectionView{
    if (!_mainCollectionView) {
        /**创建layout*/
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        /**设置item的行间距和列间距*/
        layout.minimumInteritemSpacing = 15;
        layout.minimumLineSpacing = 15;
        /**设置item的大小*/
        CGFloat itemW = (SCREEN_WIDTH-30-15)/2;
        layout.itemSize = CGSizeMake(itemW, 200);
        /*设置每个分区的上左下右的内边距*/
        layout.sectionInset = UIEdgeInsetsMake(15, 15,2, 15);
        /**创建collectionView*/
        _mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:layout];
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        _mainCollectionView.backgroundColor = ColorWithHexRGB(0x1F1E23);
        /**注册item*/
        [_mainCollectionView registerClass:[LYouSearchListCell class] forCellWithReuseIdentifier:@"searchListCellID"];
    }
    return _mainCollectionView;
}

@end
